package com.hex.ds.hdtp.core.inf.script.transfer.repository.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hex.ds.hdtp.core.inf.common.page.Page;
import com.hex.ds.hdtp.core.inf.common.page.converter.IPageAndMyBatisPlusPageConverter;
import com.hex.ds.hdtp.core.inf.data.table.po.TableTransferListPo;
import com.hex.ds.hdtp.core.inf.script.transfer.mapper.IScriptConvertSourceListMapper;
import com.hex.ds.hdtp.core.inf.script.transfer.po.ScriptConvertSourceListPo;
import com.hex.ds.hdtp.core.inf.script.transfer.repository.IScriptConvertSourceListRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Package: com.hex.ds.hdtp.core.inf.script.transfer.repository.impl
 * @ClassName ScriptConvertListRepository
 * @Description: 脚本转换清单-数据服务接口实现类
 * @Author: wz.li
 * @Date 2023/2/12 10:26
 * @Version v2.0.2
 **/
@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ScriptConvertSourceListRepository implements IScriptConvertSourceListRepository {

    private final IScriptConvertSourceListMapper scriptConvertListMapper;

    private final IPageAndMyBatisPlusPageConverter pageConverter;

    private static final String TASK_PK_ID = "convert_task_pk_id";

    /*
     * @Method: queryListByPage <br>
     * @Param: [scriptConvertListPo, page] <br>
     * @Return: com.hex.ds.hdtp.core.inf.common.page.Page<com.hex.ds.hdtp.core.inf.script.transfer.po.ScriptConvertListPo> <br>
     * @Description：分页查询<br>
     * @Author： wz.li<br>
     * @Date： 2023/2/12 10:28 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    public Page<ScriptConvertSourceListPo> queryListByPage(ScriptConvertSourceListPo scriptConvertSourceListPo, Page page) {
        final IPage tmpPageResult = scriptConvertListMapper.queryListByPage(
                pageConverter.toMyBatisPlusPage(page)
                , scriptConvertSourceListPo
        );
        final List list = tmpPageResult.getRecords();
        final Page pageResult = pageConverter.toPage(tmpPageResult);
        return pageResult.setData(list);
    }

    /*
     * @Method: queryById <br>
     * @Param: [pkId] <br>
     * @Return: com.hex.ds.hdtp.core.inf.script.transfer.po.ScriptConvertListPo <br>
     * @Description：根据主键查询<br>
     * @Author： wz.li<br>
     * @Date： 2023/2/12 10:27 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    public ScriptConvertSourceListPo queryById(String pkId) {
        return scriptConvertListMapper.selectById(pkId);
    }

    /*
     * @Method: add <br>
     * @Param: [scriptConvertListPo] <br>
     * @Return: java.lang.Integer <br>
     * @Description：新增<br>
     * @Author： wz.li<br>
     * @Date： 2023/2/12 10:27 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    public Integer add(ScriptConvertSourceListPo scriptConvertSourceListPo) {
        return scriptConvertListMapper.insert(scriptConvertSourceListPo);
    }

    /*
     * @Method: modifyById <br>
     * @Param: [scriptConvertListPo] <br>
     * @Return: java.lang.Integer <br>
     * @Description：根据主键更新<br>
     * @Author： wz.li<br>
     * @Date： 2023/2/12 10:27 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    public Integer modifyById(ScriptConvertSourceListPo scriptConvertSourceListPo) {
        return scriptConvertListMapper.updateById(scriptConvertSourceListPo);
    }

    /*
     * @Method: removeById <br>
     * @Param: [pkId] <br>
     * @Return: java.lang.Integer <br>
     * @Description：根据主键删除<br>
     * @Author： wz.li<br>
     * @Date： 2023/2/12 10:27 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    public Integer removeById(String pkId) {
        return scriptConvertListMapper.deleteById(pkId);
    }

    /*
     * @Method: removeByTaskId <br>
     * @Param: [convertTaskPkId] <br>
     * @Return: java.lang.Integer <br>
     * @Description：根据转换任务主键删除<br>
     * @Author： wz.li<br>
     * @Date： 2023/2/12 10:28 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    public Integer removeByTaskId(String convertTaskPkId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(TASK_PK_ID, convertTaskPkId);
        return scriptConvertListMapper.delete(queryWrapper);
    }

    /*
     * @Method: modifyByTaskId <br>
     * @Param: [scriptConvertListPo] <br>
     * @Return: java.lang.Integer <br>
     * @Description：根据转换任务主键更新转换清单<br>
     * @Author： wz.li<br>
     * @Date： 2023/2/13 16:09 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    public Integer modifyByTaskId(ScriptConvertSourceListPo scriptConvertSourceListPo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(TASK_PK_ID, scriptConvertSourceListPo.getConvertTaskPkId());
        return scriptConvertListMapper.update(scriptConvertSourceListPo, queryWrapper);
    }

    /*
     * @Method: queryListByTaskId <br>
     * @Param: [convertTaskPkId] <br>
     * @Return: java.util.List<com.hex.ds.hdtp.core.inf.script.transfer.po.ScriptConvertListPo> <br>
     * @Description：根据指定的转换任务主键查询转换清单<br>
     * @Author： wz.li<br>
     * @Date： 2023/2/13 11:00 <br>
     * @Version： V2.0.2<br>
     */
    @Override
    public List<ScriptConvertSourceListPo> queryListByTaskId(String convertTaskPkId, String status) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(TASK_PK_ID, convertTaskPkId);
        if (StrUtil.isNotBlank(status)){
            queryWrapper.eq("status", status);
        }
        return scriptConvertListMapper.selectList(queryWrapper);
    }

    @Override
    public List<ScriptConvertSourceListPo> queryList(ScriptConvertSourceListPo scriptConvertSourceListPo) {
        return scriptConvertListMapper.queryList(scriptConvertSourceListPo);
    }
}
